%% Author: zhangjun
%% Created: 2008-11-28
%% Description: TODO: Add description to erltest3
-module(erltest3).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([start/1]).
-export([pack/3]).
-export([startproc/0]).
-export([childproc/1]).
-export([childproc1/1]).
-export([childproc2/1]).

%%
%% API Functions
%%

childproc1(PPid) ->
    io:format("enter child1 ,parent pid is ~w~n", [PPid]),
	%link(PPid),
    process_flag(trap_exit,  true),
    receive
        {'EXIT',  Pid,  Why}  -> %will recv checked
            io:format("child1 recv ~w quit info ~w~n",[Pid,Why])
    after 1000 ->
        timeout
    end,
    io:format("child1 ~w quit ~n",[self()]),
    exit(child1die).

childproc2(PPid) ->
    io:format("enter child2 ,parent pid is ~w~n", [PPid]),
	link(PPid),
    process_flag(trap_exit,  true),
    receive
        {'EXIT',  Pid,  Why}  -> %will recv checked
            io:format("child2 recv ~w quit info ~w~n",[Pid,Why])
    after 3000 ->
        timeout
    end,
    io:format("child2 ~w quit ~n",[self()]),
    exit(child2die).

childproc(PPid) ->
    io:format("~w~n", [PPid]).

startproc() ->
    PPid = self(),
    Cpid1 = spawn_link( ?MODULE, childproc1, [PPid] ),
    spawn( ?MODULE, childproc2, [Cpid1] ),
    %spawn_link( fun()-> childproc1(PPid) end ),
    %process_flag(trap_exit,  true),
    receive
        {'EXIT',  Pid,  Why}  -> %will recv checked
            io:format("parent recv ~w quit info ~w~n",[Pid,Why]);
        _ ->
            ok
    after 500 ->
		timeout
	end,
    io:format("parent ~w quit ~n",[self()]),
    exit(parentdie).
	

%%
%% TODO: Add description of start/function_arity
%%
start(Num) -> 
	myabs(Num).
%%
%% Local Functions
%%
myabs(Num) when (Num==1) orelse (Num/2 >3)->
    ANum = 0-Num;
myabs(Num) when (Num==1) orelse (Num/2 >1)->
    ANum = 2*Num.

%integer : default size is 8, default unit is 1%
%binary  : default size is  , default unit is 8, just is a byte%
pack0(SrcID,DstID,Body) ->
    Ver = <<255:32/little>>,
    Len = length(Body),
    Len1 = <<Len:32/little>>,
    Id = <<5004:32/little>>,
    No = <<1:32/little>>,
    SrcID1 = [SrcID,<<0:((32-length(SrcID))*8)>>],
    SrcID2 = list_to_binary(SrcID1),
    DstID1 = [DstID,<<0:((32-length(SrcID))*8)>>],
    DstID2 = list_to_binary(DstID1),
    Req = [Ver,Len1,Id,No,SrcID2,DstID2,Body],  
    <<Ver1:32/little,Len2:32/little,Id1:32/little,No1:32/little,SrcID3:32/binary,DstID3:32/binary,Msg/binary>>=list_to_binary(Req), 
	io:format("output is ~w~n srcid = ~w~n destid = ~w~n", [Req,binary_to_list(SrcID3),binary_to_list(DstID3)]).

pack(SrcID,DstID,Body) ->
    put(name,"zhangjun"),
    Ver = <<255:32/little>>,
    Len = length(Body),
    Id = <<5004:32/little>>,
    No = <<1:32/little>>,
    Req = [Ver,<<Len:32/little>>,Id,No,SrcID,<<0:((32-length(SrcID))*8)>>,DstID,<<0:((32-length(SrcID))*8)>>,Body],  
    <<Ver1:32/little,Len1:32/little,Id1:32/little,No1:32/little,SrcID1:32/binary,DstID1:32/binary,Msg/binary>>=list_to_binary(Req), 
	io:format("output is ~w~n srcid = ~w~n destid = ~w~n", [Req,binary_to_list(SrcID1),binary_to_list(DstID1)]),
	get(name).

